package org.jeecg.modules.icon.check;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
 * @ProjectName: labour-server
 * @ClassName: com.hckj.kq.api.oms.action.ParamCheckUtils
 * @Version: 1.0
 */
@Slf4j
public class ParamCheckUtils {

    public static void check(Object paramObj) {
        Class clazz = paramObj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        if (ArrayUtil.isNotEmpty(fields)) {
            List<Field> fieldList = Arrays.asList(fields);
            fieldList.stream().forEach(item -> {
                CheckParam checkParam = item.getAnnotation(CheckParam.class);
                if (ObjectUtil.isNotNull(checkParam)) {
                    item.setAccessible(true);
                    Object fieldValue = ReflectionUtils.getField(item, paramObj);
                    if (ObjectUtil.isNull(fieldValue)) {
                        log.error(item.getName() + "参数校验失败!");
                        throw new ParamCheckException(item.getName() + "参数校验失败!");
                    }
                }
            });
        }
    }
}